package com.material.web.controller.factory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.material.common.exception.ServiceException;
import com.material.common.utils.SecurityUtils;
import com.material.common.utils.poi.ExcelTemplateUtil;
import com.material.factory.domain.FactoryFuelAttribute;
import com.material.factory.service.IFactoryFuelAttributeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.material.common.annotation.Log;
import com.material.common.core.controller.BaseController;
import com.material.common.core.domain.AjaxResult;
import com.material.common.enums.BusinessType;
import com.material.factory.domain.FactoryFuel;
import com.material.factory.service.IFactoryFuelService;
import com.material.common.utils.poi.ExcelUtil;
import com.material.common.core.page.TableDataInfo;

/**
 * 工厂燃料品名Controller
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/factory/fuel")
public class FactoryFuelController extends BaseController
{
    @Autowired
    private IFactoryFuelService factoryFuelService;
    @Autowired
    private IFactoryFuelAttributeService factoryFuelAttributeService;

    /**
     * 查询工厂燃料品名列表
     */
    @PreAuthorize("@ss.hasPermi('factory:fuel:list')")
    @GetMapping("/list")
    public TableDataInfo list(FactoryFuel factoryFuel)
    {
        factoryFuel.setState(1);
        Long bizId = getBizId();
        boolean admin = SecurityUtils.isAdmin(getUserId());
        if(!admin){
            factoryFuel.setFactoryId(bizId);
        }
        startPage();
        List<FactoryFuel> list = factoryFuelService.selectFactoryFuelList(factoryFuel);
        return getDataTable(list);
    }

    /**
     * 导出工厂燃料品名列表
     */
    @PreAuthorize("@ss.hasPermi('factory:fuel:export')")
    @Log(title = "工厂燃料品名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FactoryFuel factoryFuel)
    {
        List<FactoryFuel> list = factoryFuelService.selectFactoryFuelList(factoryFuel);
        ExcelUtil<FactoryFuel> util = new ExcelUtil<FactoryFuel>(FactoryFuel.class);
        util.exportExcel(response, list, "工厂燃料品名数据");
    }

    /**
     * 获取工厂燃料品名详细信息
     */
    @PreAuthorize("@ss.hasPermi('factory:fuel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(factoryFuelService.selectFactoryFuelById(id));
    }

    /**
     * 新增工厂燃料品名
     */
    @PreAuthorize("@ss.hasPermi('factory:fuel:add')")
    @Log(title = "工厂燃料品名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FactoryFuel factoryFuel)
    {
        Long bizId = getBizId();
        if(bizId == 0){
            return error("请使用工厂管理员账号操作");
        }
        factoryFuel.setFactoryId(bizId);
        return toAjax(factoryFuelService.insertFactoryFuel(factoryFuel));
    }

    /**
     * 修改工厂燃料品名
     */
    @PreAuthorize("@ss.hasPermi('factory:fuel:edit')")
    @Log(title = "工厂燃料品名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FactoryFuel factoryFuel)
    {
        return toAjax(factoryFuelService.updateFactoryFuel(factoryFuel));
    }

    /**
     * 删除工厂燃料品名
     */
    @PreAuthorize("@ss.hasPermi('factory:fuel:remove')")
    @Log(title = "工厂燃料品名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(factoryFuelService.deleteFactoryFuelByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response,Long id)
    {
        FactoryFuelAttribute query = new FactoryFuelAttribute();
        query.setFuelId(id);
        List<FactoryFuelAttribute> factoryFuelAttributes = factoryFuelAttributeService.selectFactoryFuelAttributeList(query);
        if(CollectionUtils.isEmpty(factoryFuelAttributes)){
            throw new ServiceException("燃料品名属性未配置");
        }
        List<String> list = new ArrayList<>();
        list.add("送达日期");
        list.add("供应商");
        list.add("燃料品名");
        list.add("车牌号");
        List<String> collect = factoryFuelAttributes.stream().map(FactoryFuelAttribute::getAttrName).collect(Collectors.toList());
        list.addAll(collect);
        ExcelTemplateUtil.getTemplate(response,"化验结果",list);
    }
}
